package net.csdn.business.auth.feign;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.csdn.business.auth.facade.AuthFacade;
import net.csdn.business.common.annotation.Inner;
import net.csdn.business.common.domain.request.oauth.*;
import net.csdn.business.common.domain.vo.oauth.OauthClientInfoVo;
import net.csdn.business.common.domain.vo.oauth.OauthStatVO;
import net.csdn.business.common.oauth2.service.IOauthAccessTokenService;
import net.csdn.business.common.oauth2.service.IOauthClientInfoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @Desc: Tokencontroller
 * @ClassName: TokenController
 * @Author: yourleige@163.com
 * @Date: 2022/9/27 11:16
 */
@Slf4j
@Inner
@RestController
@RequestMapping("/api/v1/client")
@RequiredArgsConstructor
public class AuthClientController
{
    private final AuthFacade authFacade;

    private final IOauthClientInfoService oauthClientInfoService;

    /**
     * @Desc: 分页查询我创建的oauth应用列表
     * @Author: zhangyalei
     * @Date: 2023/1/11 15:18
     */
    @PostMapping("/myList")
    public Page<OauthClientInfoVo> findMyPageList(@RequestBody OauthClientInfoQuery query){
        return oauthClientInfoService.findPageList(query);
    }

    /**
     * @Desc: 分页查询我已经授权的oauth应用列表
     * @Author: zhangyalei
     * @Date: 2023/1/11 15:18
     */
    @PostMapping("/authList")
    public Page<OauthClientInfoVo> findAuthorizeList(@RequestBody OauthClientInfoQuery query){
        return oauthClientInfoService.findAuthorizeList(query);
    }


    /**
     * @Desc: 根据clientId查询oauth应用详细信息
     * @Author: zhangyalei
     * @Date: 2023/1/11 15:18
     */
    @GetMapping("/{clientId}")
    public OauthClientInfoVo getClientByClientId(@PathVariable("clientId") String clientId,@RequestParam("isWithSecret") Boolean isWithSecret){
        if(isWithSecret==null){
            //默认脱敏
            isWithSecret=false;
        }
        return oauthClientInfoService.selectClientByClientId(clientId,isWithSecret);
    }



    /**
     * @Desc: 新增client
     * @Author: zhangyalei
     * @Date: 2023/1/11 15:18
     */
    @PostMapping("/addClient")
    public String addClient(@RequestBody NewOauthClientInfoDTO params){
        return oauthClientInfoService.insertOauthClientInfo(params);
    }



    /**
     * @Desc: 更新client
     * @Author: zhangyalei
     * @Date: 2023/1/11 15:18
     */
    @PostMapping("/updateClient")
    public Integer updateClient(@RequestBody UpdateOauthClientInfoDTO params){
        return oauthClientInfoService.updateOauthClientInfo(params);
    }


    /**
     * @Desc: client重新生成密钥
     * @Author: zhangyalei
     * @Date: 2023/1/11 15:18
     */
    @PostMapping("/generateSecret")
    public String generateSecret(@RequestBody NewOauthClientSecretDTO params){
        return oauthClientInfoService.generateSecret(params);
    }

    /**
     * @Desc: 根据clientId删除应用
     * @Author: zhangyalei
     * @Date: 2023/1/11 15:18
     */
    @PostMapping("/deleteByClientId")
    public Integer deleteByClientId(@RequestParam("clientId")String clientId){
        return oauthClientInfoService.deleteByClientId(clientId);
    }

    /**
     * @Desc: 根据userId、clientId撤销用户对此client的授权
     * @Author: zhangyalei
     * @Date: 2023/1/11 15:18
     */
    @PostMapping("/revoke")
    public Integer revoke(@Validated @RequestBody OauthRevokeDTO params){
        return authFacade.revoke(params);
    }

    /**
     * @Desc: 根据clientId撤销所有用户的授权
     * @Author: zhangyalei
     * @Date: 2023/1/11 15:18
     */
    @PostMapping("/revokeAll")
    public Integer revokeAll(@Validated @RequestBody OauthRevokeAllDTO params){
        return authFacade.revokeAll(params);
    }


    /**
     * @Desc: 根据clientId查询应用的授权记录统计信息
     * @Author: zhangyalei
     * @Date: 2023/1/11 15:18
     */
    @GetMapping("/authStat/{clientId}")
    public OauthStatVO authStat(@PathVariable("clientId")String clientId){
        return oauthClientInfoService.authStat(clientId);
    }
}
